package org.db.ui;

import java.awt.Component;
import java.awt.FocusTraversalPolicy;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.db.Manager;
import org.db.dao.HoehenlinienDAO;
import org.db.dao.change.AddProjektElement;
import org.db.dao.change.RemoveProjektElement;
import org.db.dao.change.UpdateProjektElement;

/**
 * 
 * @author Lutz
 */
public class HoehenlinienDialog extends CloseCommitDialog
		implements
			ActionListener,
			FocusListener,
			KeyListener,
			MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6580592501210283232L;

	private HoehenlinienDAO lastAddedDao;

	private DecimalFormat format1 = new DecimalFormat("0.000");

	private DecimalFormat format2 = new DecimalFormat("0.00");

	private HoehenlinienDAO element2Edit = null;

	public HoehenlinienDialog(JDialog owner) {
		super(owner);
		setModal(true);
		setTitle("Höhenlinien");
		initComponents();
	}

	private static final String AException = "Bitte geben Sie einen Wert für ra ein!";

	private static final String HoehenException = "Bitte geben Sie eine Höhe ein!";

	private static final String NeigungException = "Bitte geben Sie die Neigung und die Länge der Neigung ein!";

	private static final String StationException = "Bitte geben Sie eine Stationierung ein!";

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jTextFieldStation = new DoubleTextField();
		jLabel11 = new javax.swing.JLabel();
		jLabel12 = new javax.swing.JLabel();
		jTextNeigungAb = new DoubleTextField(true);
		jTextNeigungLaengeAb = new DoubleTextField();
		jTextRa = new IntegerTextField();
		jLabel7 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel8 = new javax.swing.JLabel();
		jLabel17 = new javax.swing.JLabel();
		jLabel13 = new javax.swing.JLabel();
		jLabel14 = new javax.swing.JLabel();
		jTextNeigungAn = new DoubleTextField(true);
		// jTextNeigungAn.addFocusListener(this);
		jLabel16 = new javax.swing.JLabel();
		jTextHoehe = new DoubleTextField();
		jLabel5 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jTextNeigungLaengeAn = new DoubleTextField();
		// jTextNeigungLaengeAn.addFocusListener(this);
		jLabel10 = new javax.swing.JLabel();
		jButtonOK = new javax.swing.JButton();
		jButtonNextNW = new javax.swing.JButton();
		jButtonCancel = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		table = new HoehenlinienTable();
		table.addKeyListener(this);
		table.addMouseListener(this);
		jLabelGleistyp = new javax.swing.JLabel();
		jLabelGleistyp.setText(Manager.getInstance().getCurrentGleis()
				.toString());

		setPreferredSize(new java.awt.Dimension(800, 520));
		setResizable(false);

		jTextFieldStation.setVisible(false);

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
		jLabel1.setText("Höhenlinie");

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel3.setText("Stationierung");
		jLabel3.setVisible(false);

		jLabel11.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel11.setText("abgehende Neigung");
		jLabel11.setVisible(false);

		jLabel12.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel12.setText("Länge");
		jLabel12.setVisible(false);

		jTextNeigungAb.setVisible(false);

		jTextNeigungLaengeAb.setVisible(false);

		jTextRa.setToolTipText("nur bei NW mit Ausrundung");
		jTextRa.setVisible(false);

		jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel7.setText(" ankommende Neigung");

		jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel6.setText("m");
		jLabel6.setVisible(false);

		jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel8.setText("‰");

		jLabel17.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel17.setText("ra");
		jLabel17.setVisible(false);

		jLabel16.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel16.setText("m");
		jLabel16.setVisible(false);

		jTextHoehe.setToolTipText("Höhe Neigungswechsel");
		jTextHoehe.setVisible(false);

		jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel5.setText("Höhe- NW");
		jLabel5.setVisible(false);

		jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel9.setText("Länge");

		jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel10.setText("m");

		jButtonOK.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonOK.setText("Eingabe beenden");
		jButtonOK
				.setToolTipText("Prüfen und Speichern der Eingaben. Zurück zum Auswahlfenster.");
		jButtonOK.addActionListener(this);
		jButtonOK.addKeyListener(this);

		jButtonNextNW.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonNextNW.setText("nächstes Element");
		jButtonNextNW
				.setToolTipText("Speichern der Eingaben in die Liste. Leeren der Textfelder für nächstes Element.");
		jButtonNextNW.addActionListener(this);
		jButtonNextNW.addKeyListener(this);

		jButtonCancel.setFont(new java.awt.Font("Tahoma", 1, 12));
		jButtonCancel.setText("Abbrechen");
		jButtonCancel
				.setToolTipText("Verwerfen der Eingaben, zurück zum Hauptmen.ü");
		jButtonCancel.addActionListener(this);
		jButtonCancel.addKeyListener(this);

		jLabel13.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel13.setText("‰");
		jLabel13.setVisible(false);

		jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12));
		jLabel14.setText("m");
		jLabel14.setVisible(false);

		jScrollPane1.setViewportView(table);

		jLabelGleistyp.setFont(new java.awt.Font("Tahoma", 3, 12));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addContainerGap(
																		1021,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jButtonOK,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										150,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jButtonCancel,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										150,
																										javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						766,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						jButtonNextNW,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						150,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addContainerGap(
																		353,
																		Short.MAX_VALUE))
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jLabel9)
																				.addComponent(
																						jLabel7))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING,
																				false)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextNeigungAn,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										63,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel8)
																								.addGap(38,
																										38,
																										38)
																								.addComponent(
																										jLabel5))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextNeigungLaengeAn,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										63,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel10)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										jLabel17))
																				.addComponent(
																						jLabel3,
																						javax.swing.GroupLayout.Alignment.TRAILING))
																.addGap(4, 4, 4)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														jTextRa,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														63,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														jTextHoehe,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														63,
																														javax.swing.GroupLayout.PREFERRED_SIZE))
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel6)
																								.addGap(47,
																										47,
																										47))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextFieldStation,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										63,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel16)))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(
																						jLabel11)
																				.addComponent(
																						jLabel12))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextNeigungAb,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										63,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel13))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										jTextNeigungLaengeAb,
																										javax.swing.GroupLayout.PREFERRED_SIZE,
																										63,
																										javax.swing.GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										jLabel14)))
																.addGap(453,
																		453,
																		453))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabelGleistyp,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		252,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addContainerGap(
																		867,
																		Short.MAX_VALUE)))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addGap(18, 18, 18)
								.addComponent(jLabelGleistyp)
								.addGap(44, 44, 44)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel7)
												.addComponent(
														jTextNeigungAn,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel8)
												.addComponent(jLabel5)
												.addComponent(
														jTextHoehe,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel6)
												.addComponent(jLabel11)
												.addComponent(
														jTextNeigungAb,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel13))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel9)
												.addComponent(
														jTextNeigungLaengeAn,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel10)
												.addComponent(jLabel17)
												.addComponent(
														jTextRa,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel12)
												.addComponent(
														jTextNeigungLaengeAb,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel14))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(
														jTextFieldStation,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel16))
								.addGap(44, 44, 44)
								.addComponent(jButtonNextNW,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										37,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										136,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														jButtonCancel,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														34,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jButtonOK,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														37,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(25, 25, 25)));
		continueInput();
		validateInput();
		isValidSession();
		pack();
	}

	/**
	 * Eingabefelder mit dem letzten Datensatz füllen und für den nächsten NW
	 * vorbereiten
	 */
	public void continueInput() {
		if (!table.isEmpty()) {
			lastAddedDao = Manager.getInstance().getHoehenlinienElemente()
					.last();
			jTextHoehe.setVisible(true);
			jTextHoehe.setText(Double.valueOf(lastAddedDao.gethoehe())
					.toString());
			jTextRa.setVisible(true);
			jTextFieldStation.setVisible(true);
			jTextFieldStation.setText(Double.valueOf(
					lastAddedDao.getStationierungInMeter()).toString());
			jLabel5.setVisible(true);
			jLabel6.setVisible(true);
			jLabel17.setVisible(true);
			jLabel16.setVisible(true);
			jLabel3.setVisible(true);
			jTextNeigungAb.setVisible(true);
			jTextNeigungAb.setText(Double.valueOf(lastAddedDao.getNeigungAb())
					.toString());
			jTextNeigungLaengeAb.setVisible(true);
			jTextNeigungLaengeAb.setText(Double.valueOf(
					lastAddedDao.getLaengeAb()).toString());
			jLabel11.setVisible(true);
			jLabel12.setVisible(true);
			jLabel13.setVisible(true);
			jLabel14.setVisible(true);
			jTextNeigungAn.setText(Double.valueOf(lastAddedDao.getNeigungAn())
					.toString());
			jTextNeigungLaengeAn.setText(Double.valueOf(
					lastAddedDao.getLaengeAn()).toString());

			clearInput();
			jTextFieldStation.requestFocusInWindow();
		}
	}

	public int showHoehenlinienDialog() {
		setVisible(true);
		return returnValue;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jButtonOK) {

			if (isValidSession()) {
				// Änderungen in XML-Datei übernehmen
				Manager.getInstance().applyChanges();
				setVisible(false);
				dispose();
			} else {
				JOptionPane.showMessageDialog(this,
						"Bitte überprüfen Sie die gelben Fehler!");
			}
		} else if (e.getSource() == jButtonCancel) {
			returnValue = JOptionPane.CANCEL_OPTION;
			askForClosing();

		} else if (e.getSource() == jButtonNextNW) {
			if (!jTextHoehe.isVisible() && isValidInputAnNeigung()) {
				ChangeVisibilityNW();
			}

			else if (jTextHoehe.isVisible() && !jTextNeigungAb.isVisible()
					&& isValidInputNW()) {
				ChangeVisibilityAbNeigung();
			} else if (isValidInputAbNeigung()) {

				double stationierungInMeter = Double.valueOf(jTextFieldStation
						.getText());
				double hoehe = Double.valueOf(jTextHoehe.getText());
				double neigungAn = Double.valueOf(jTextNeigungAn.getText());
				double laengeAn = Double
						.valueOf(jTextNeigungLaengeAn.getText());
				int ra = Math.abs(Double.valueOf(jTextNeigungAb.getText())
						- Double.valueOf(jTextNeigungAn.getText())) >= 1
						? Integer.valueOf(jTextRa.getText())
						: 0;
				double neigungAb = Double.valueOf(jTextNeigungAb.getText());
				double laengeAb = Double
						.valueOf(jTextNeigungLaengeAb.getText());

				double lt = (ra != 0) ? ((ra / 2) * Math.abs(neigungAb
						- neigungAn)) / 1000 : 0;
				double a = (ra != 0)
						? ((lt / 4) * (neigungAb - neigungAn)) / 1000
						: 0;

				HoehenlinienDAO dao = new HoehenlinienDAO(neigungAn, laengeAn,
						hoehe, stationierungInMeter, ra, lt, a, neigungAb,
						laengeAb);

				if (element2Edit != null) {
					table.replaceHoehenlinienElement(element2Edit, dao);
					Manager.getInstance().addProjektChange(
							new UpdateProjektElement(element2Edit, dao));
					element2Edit = null;
				} else {
					table.addHoehenlinienElement(dao);
					Manager.getInstance().addProjektChange(
							new AddProjektElement(dao));
				}

				clearInput();
				returnValue = JOptionPane.OK_OPTION;
				lastAddedDao = dao;
				validateInput();
				isValidSession();
			}
		}
		updateFocusTraversalOrder();
	}

	/**
	 * Erzwingen von mindestens einer kompletten Eingabe
	 */
	private void validateInput() {
		jButtonOK.setEnabled(!table.isEmpty());
	}

	/**
	 * darstellen von weiteren Textfeldern nach der ersten Eingabe
	 */
	private void ChangeVisibilityNW() {

		jTextHoehe.setVisible(true);
		jTextRa.setVisible(true);
		jTextFieldStation.setVisible(true);
		jLabel5.setVisible(true);
		jLabel6.setVisible(true);
		jLabel17.setVisible(true);
		jLabel16.setVisible(true);
		jLabel3.setVisible(true);
		jTextHoehe.requestFocusInWindow();

	}

	/**
	 * darstellen von weiteren Textfeldern nach der zweiten Eingabe
	 */
	private void ChangeVisibilityAbNeigung() {

		jTextNeigungAb.setVisible(true);
		jTextNeigungLaengeAb.setVisible(true);
		jLabel11.setVisible(true);
		jLabel12.setVisible(true);
		jLabel13.setVisible(true);
		jLabel14.setVisible(true);
		jTextNeigungAb.requestFocusInWindow();

	}

	/**
	 * Test ob die Änderungen in der aktuellen Sitzung gültig sind.
	 * 
	 * @return
	 */
	private boolean isValidSession() {
		table.resetHighlighting();
		List<HoehenlinienDAO> hoehenlinienElemente = table
				.getHoehenlinienElemente();
		HoehenlinienDAO el1;
		HoehenlinienDAO el2;
		for (int i = 0; i < hoehenlinienElemente.size() - 1; i++) {
			el1 = hoehenlinienElemente.get(i);
			el2 = hoehenlinienElemente.get(i + 1);
			if (el1.getNeigungAb() != el2.getNeigungAn()) {
				table.highlightCell(i, 7);
				table.highlightCell(i + 1, 0);
				return false;
			}
			if (el1.getLaengeAb() != el2.getLaengeAn()) {
				table.highlightCell(i, 8);
				table.highlightCell(i + 1, 1);
				return false;
			}
			if (Math.abs(el1.getStationierungInMeter()
					- el2.getStationierungInMeter() + el1.getLaengeAb()) >= 0.2) {
				table.highlightCell(i, 3);
				table.highlightCell(i, 8);
				table.highlightCell(i + 1, 3);
				return false;
			}
			if (Math.abs(el2.gethoehe() - el1.gethoehe()
					- (el1.getNeigungAb() * el1.getLaengeAb()) / 1000) >= 0.01) {
				table.highlightCell(i, 2);
				table.highlightCell(i, 7);
				table.highlightCell(i, 8);
				table.highlightCell(i + 1, 2);
				return false;
			}
		}
		return true;
	}

	/**
	 * Test ob die aktuellen Eingaben gültig sind.
	 * 
	 * @return
	 */
	private boolean isValidInputAnNeigung() {
		if (jTextNeigungAn.getText().isEmpty()) {
			JOptionPane.showMessageDialog(this, NeigungException,
					"Neigung und Länge", JOptionPane.WARNING_MESSAGE);
			return false;
		} else if (jTextNeigungLaengeAn.getText().isEmpty()) {
			JOptionPane.showMessageDialog(this, NeigungException,
					"Neigung und Länge", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		return true;
	}

	private boolean isValidInputNW() {
		if (jTextFieldStation.isVisible()) {
			if (jTextFieldStation.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, StationException,
						"Fehlende Stationierung", JOptionPane.WARNING_MESSAGE);
				return false;
			} else if (jTextHoehe.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, HoehenException,
						"Fehlende Höhe", JOptionPane.WARNING_MESSAGE);
				return false;
			}
		}
		return true;
	}

	/**
	 * Prüfung der vollständigen Eingabe für abgehende Neigung
	 */
	private boolean isValidInputAbNeigung() {
		if (jTextNeigungAb.isVisible()) {
			if (jTextNeigungAb.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, NeigungException,
						"Neigung und Länge", JOptionPane.WARNING_MESSAGE);
				return false;
			} else if (jTextNeigungLaengeAb.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, NeigungException,
						"Neigung und Länge", JOptionPane.WARNING_MESSAGE);
				return false;
			} else if (Math.abs(Double.valueOf(jTextNeigungAb.getText())
					- Double.valueOf(jTextNeigungAn.getText())) >= 1
					&& jTextRa.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, AException, "Parameter ra",
						JOptionPane.WARNING_MESSAGE);
				jTextRa.requestFocusInWindow();
				return false;
			}
		}
		return true;
	}

	/**
	 * Leeren und Vorbereiten der Textfelder für folgenden NW
	 */
	private void clearInput() {

		Double stationNeu = Double.valueOf(jTextFieldStation.getText())
				+ Double.valueOf(jTextNeigungLaengeAb.getText());
		jTextFieldStation.setText(format2.format(stationNeu));
		String hoeheNeu = format1.format((Double.valueOf(jTextNeigungLaengeAb
				.getText()) * Double.valueOf(jTextNeigungAb.getText()))
				/ 1000
				+ Double.valueOf(jTextHoehe.getText()));
		jTextHoehe.setText(hoeheNeu);
		jTextNeigungAn.setText(jTextNeigungAb.getText());
		jTextNeigungLaengeAn.setText(jTextNeigungLaengeAb.getText());
		jTextNeigungAb.setText("");
		jTextNeigungLaengeAb.setText("");
		jTextRa.setText("");
		jTextFieldStation.requestFocusInWindow();
	}

	/**
	 * Löschen der ausgewählten Reihe der Tabelle
	 */
	private void deleteSelectedRow() {
		int selectedRowIndex = table.getSelectedRow();
		if (selectedRowIndex > -1) {
			HoehenlinienDAO removedDAO = table
					.removeHoehenlinienElement(selectedRowIndex);
			Manager.getInstance().addProjektChange(
					new RemoveProjektElement(removedDAO));
			validateInput();
		}

	}

	public static String getAusrundungsError() {
		return AException;
	}

	public static String getAusrundungsNWError() {
		return HoehenException;
	}

	public static String getNeigungError() {
		return NeigungException;
	}

	public static String getStationError() {
		return StationException;
	}

	private int returnValue = 0;

	private javax.swing.JButton jButtonCancel;

	private javax.swing.JButton jButtonNextNW;

	private javax.swing.JButton jButtonOK;

	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel10;

	private javax.swing.JLabel jLabel11;

	private javax.swing.JLabel jLabel12;

	private javax.swing.JLabel jLabel13;

	private javax.swing.JLabel jLabel14;

	private javax.swing.JLabel jLabel16;

	private javax.swing.JLabel jLabel17;

	private javax.swing.JLabel jLabel3;

	private javax.swing.JLabel jLabel5;

	private javax.swing.JLabel jLabel6;

	private javax.swing.JLabel jLabel7;

	private javax.swing.JLabel jLabel8;

	private javax.swing.JLabel jLabel9;

	private javax.swing.JScrollPane jScrollPane1;

	private HoehenlinienTable table;

	private IntegerTextField jTextRa;

	private DoubleTextField jTextFieldStation;

	private DoubleTextField jTextHoehe;

	private DoubleTextField jTextNeigungAn;

	private DoubleTextField jTextNeigungLaengeAn;

	private DoubleTextField jTextNeigungAb;

	private DoubleTextField jTextNeigungLaengeAb;

	private javax.swing.JLabel jLabelGleistyp;

	@Override
	public void focusGained(FocusEvent arg0) {

	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == jTextNeigungAn) {
			updateTable();
		} else if (e.getSource() == jTextNeigungLaengeAn) {
			updateTable();
		}

	}

	/**
	 * Updaten des ausgewählten Elements
	 */
	private void updateTable() {
		if (lastAddedDao != null) {
			if (!jTextNeigungAn.getText().isEmpty()) {
				lastAddedDao.setNeigungAn(Double.valueOf(jTextNeigungAn
						.getText()));
			}
			if (!jTextNeigungLaengeAn.getText().isEmpty()) {
				lastAddedDao.setLaengeAn(Double.valueOf(jTextNeigungLaengeAn
						.getText()));
			}
		}
		table.refresh(lastAddedDao);
	}

	/**
	 * Tabulatorreihenfolge
	 */
	private void updateFocusTraversalOrder() {
		FocusTraversalPolicy policy = null;
		if (jTextHoehe.isVisible() && !jTextNeigungAb.isVisible()) {
			policy = new CustomFocusTraversalPolicy(new Component[]{jTextHoehe,
					jTextRa, jTextFieldStation, jButtonNextNW});
		} else if (jTextNeigungAb.isVisible()) {
			policy = new CustomFocusTraversalPolicy(new Component[]{
					jTextFieldStation, jTextNeigungAb, jTextNeigungLaengeAb,
					jButtonNextNW});
		}
		setFocusTraversalPolicy(policy);
		setFocusCycleRoot(true);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_DELETE) {
			deleteSelectedRow();
		} else if (e.getSource() == jButtonNextNW) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonNextNW.doClick();
			}
		} else if (e.getSource() == jButtonCancel) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonCancel.doClick();
			}
		} else if (e.getSource() == jButtonOK) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				jButtonOK.doClick();
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// bei Doppelklick auf eine Zeile in der Tabelle die Daten des
		// zugehörigen Elements in den Eingabefeldern anzeigen
		if (e.getClickCount() == 2) {
			int row = table.rowAtPoint(e.getPoint());
			element2Edit = table.getElementAtRow(row);
			if (element2Edit != null) {
				loadElement(element2Edit);
			}
		}
	}

	/**
	 * Laden der Elemente aus der XML
	 * 
	 * @param dao
	 */
	private void loadElement(HoehenlinienDAO dao) {
		jTextNeigungAn.setValue(dao.getNeigungAn());
		jTextNeigungLaengeAn.setValue(dao.getLaengeAn());
		jTextHoehe.setValue(dao.gethoehe());
		jTextRa.setText("");
		if (dao.getRa() != 0) {
			jTextRa.setValue(dao.getRa());
		} else {
			jTextRa.setValue(0);
		}
		jTextFieldStation.setValue(dao.getStationierungInMeter());
		jTextNeigungAb.setValue(dao.getNeigungAb());
		jTextNeigungLaengeAb.setValue(dao.getLaengeAb());
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

}
